package com.rexolar.openapi.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.rexolar.openapi.db.entity.SaasUserInfo;
import com.rexolar.openapi.db.mapper.SaasUserInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AuthCheckService
 * @Description:
 * @Author: zhusiyu
 * @CreateDate: 2024/12/19 10:39	//创建时间
 */
@Service
public class AuthCheckService {
    @Resource
    SaasUserInfoMapper saasUserInfoMapper;

    @Resource
    RexolarApiClient rexolarApiClient;

    /**
     * 校验用户邮箱是否存在
     * @param token)
     * @return
     */
    public boolean checkToken(String token){
        if (StringUtils.isBlank(token)){
            return false;
        }
        Map<String, Object> userMap = rexolarApiClient.getAuthUser(token);
        if (CollectionUtils.isEmpty(userMap)){
            return false;
        }
        LambdaQueryWrapper<SaasUserInfo> allQueryWrapper = new LambdaQueryWrapper<>();
        allQueryWrapper.eq(SaasUserInfo::getUsername, userMap.get("email"));
        List<SaasUserInfo> list = saasUserInfoMapper.selectList(allQueryWrapper);
        if (!CollectionUtils.isEmpty(list)){
            return true;
        }
        return false;
    }
}
